﻿using Com.Lancetinc.Evs.Data;
using Com.Lancetinc.Evs.Events;
using Com.Lancetinc.Evs.Helpers;
using Com.Lancetinc.Evs.Properties;
using Com.Lancetinc.Evs.Services;
using Com.Lancetinc.Evs.ViewModels;
using Prism.Events;
using System;
using System.Linq;
using System.Windows;
using MahApps.Metro.IconPacks;

namespace Com.Lancetinc.Evs.Controls
{
    /// <summary>
    /// 输入病人信息
    /// </summary>
    public partial class InputPatientUserControl
    {
        public ExperViewModel ExperViewModel => this.DataContext as ExperViewModel;

        public event EventHandler SaveSucceed;

        public InputPatientUserControl()
        {
            InitializeComponent();
            InitSexComboBox();
        }

        private bool _canDelete = true;
        public bool CanDelete
        {
            get => _canDelete;
            set
            {
                _canDelete = value;
                DeleteButton.Visibility = _canDelete ? Visibility.Visible : Visibility.Collapsed;
                ResetButtonsPanel();
            }
        }

        private bool _canPrint = true;
        public bool CanPrint
        {
            get => _canPrint;
            set
            {
                _canPrint = value;
                BtnPrintReport.Visibility = _canPrint ? Visibility.Visible : Visibility.Collapsed;
                ResetButtonsPanel();
            }
        }

        /// <summary>
        /// 0:文字
        /// 1:图标
        /// </summary>
        public int ButtonStyle { get; set; } = 0;

        public event EventHandler ExperDeleted;

        public event EventHandler RequirePrint;

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (ExperViewModel == null)
                return;
            try
            {
                AppRepositories.Exper.Save(ExperViewModel.Exper);
                // 病人信息已保存
                ShowMessage(I18nHelper.Default.GetMessage("InputPageUserControl.PatientInfoSaved"));
                SaveSucceed?.Invoke(this, EventArgs.Empty);
            }
            catch (Exception ex)
            {
                MessageHelper.ShowError(ex.Message, I18nHelper.Default.GetMessage("InputPageUserControl.SaveFailed"));
            }
        }

        private async void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (!CanDelete)
                return;

            if (!await ExperManager.Default.DeleteExper(ExperViewModel.SeqNo))
                return;

            ExperDeleted?.Invoke(sender, EventArgs.Empty);
        }

        private void ThisCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            KnownAppEvents.ComboBoxAutoCompleteItemsChangedEvent.Subscribe(ComboBoxItemsChanged, ThreadOption.UIThread);
            CtrlAttendingDoctor.ItemsSource =
                ComboBoxSelectItemsHelper.ToItemsArray(Settings.Default.AttendingDoctorItems);
            CtrlMediastinus.ItemsSource = ComboBoxSelectItemsHelper.ToItemsArray(Settings.Default.MediastinusItems);
        }

        private void ComboBoxItemsChanged(ComboBoxAutoCompleteItemsChangedEventArgs e)
        {
            if (e.Name == "AttendingDoctor")
            {
                CtrlAttendingDoctor.ItemsSource =
                    ComboBoxSelectItemsHelper.ToItemsArray(Settings.Default.AttendingDoctorItems);
            }
            else if (e.Name == "Mediastinus")
            {
                CtrlMediastinus.ItemsSource = ComboBoxSelectItemsHelper.ToItemsArray(Settings.Default.MediastinusItems);
            }
        }

        private void ShowMessage(string msg)
        {
            SbMsg.Content = msg;
            SbMain.IsActive = true;
            WpfHelper.ExecLater(() =>
            {
                SbMain.IsActive = false;
                SbMsg.Content = string.Empty;
            }, 3000);
        }

        private void InitSexComboBox()
        {
            CmbSex.ItemsSource = new ComboBoxItemViewModel<string>[]
            {
                new (I18nHelper.Default.GetMessage("Male"), "1"),
                new (I18nHelper.Default.GetMessage("Female"), "0"),
            };
        }

        private void BtnPrintReport_OnClick(object sender, RoutedEventArgs e)
        {
            RequirePrint?.Invoke(this, EventArgs.Empty);
        }

        private void ResetButtonsPanel()
        {
            // 图标
            if (ButtonStyle == 1 && DeleteButton.Content is not PackIconLevs)
            {
                const int iconSize = 30;
                DeleteButton.Content = new PackIconLevs
                {
                    Kind = PackIconLevsKind.Delete,
                    Width = iconSize,
                    Height = iconSize,
                    ToolTip = I18nHelper.Default.GetMessage("Button.Delete")
                };
                SaveButton.Content = new PackIconLevs
                {
                    Kind = PackIconLevsKind.Save,
                    Width = iconSize,
                    Height = iconSize,
                    ToolTip = I18nHelper.Default.GetMessage("Button.Save")
                };
                BtnPrintReport.Content = new PackIconLevs
                {
                    Kind = PackIconLevsKind.Printer,
                    Width = iconSize,
                    Height = iconSize,
                    ToolTip = I18nHelper.Default.GetMessage("Button.Print")
                };
            }

            PanelButtons.Columns = PanelButtons.Children.OfType<FrameworkElement>().Count(x => x.IsVisible);
        }
    }
}